package com.mti.epivisitemobile.view
{
	import com.mti.epivisite.common.event.EpivisiteEvent;
	import com.mti.epivisitemobile.Constants;
	import com.mti.epivisitemobile.model.EventListProxy;
	import com.mti.epivisitemobile.model.EventListProxyBase;
	
	import flash.events.GeolocationEvent;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.events.FlexEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class EventListViewMediator extends Mediator
	{
		public static const NAME:String = "EventListViewMediator";
		
		public function EventListViewMediator(view:EventListView)
		{
			super(NAME, view);
			
			viewComponent.addEventListener(FlexEvent.CREATION_COMPLETE, handleViewCreated);
		}
		
		private function handleViewCreated(evt:FlexEvent):void
		{
			view.removeEventListener(FlexEvent.CREATION_COMPLETE, handleViewCreated);
			
			var proxy:EventListProxyBase = facade.retrieveProxy(EventListProxyBase.NAME) as EventListProxyBase;
			proxy.getEventList();
			
			view.eventList.addEventListener(MouseEvent.CLICK, onEventListClick);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				EventListProxyBase.LOAD_SUCCESS,
			];
		}
		
		override public function handleNotification(note:INotification):void
		{
			switch (note.getName())
			{
				case EventListProxyBase.LOAD_SUCCESS:
					var proxy:EventListProxyBase = facade.retrieveProxy(EventListProxyBase.NAME) as EventListProxyBase;
					view.eventList.dataProvider = proxy.events; 
					break;
			}
		}
		
		private function onEventListClick(event:MouseEvent):void
		{
			var selectedItem:Object = view.eventList.selectedItem;
			if (selectedItem != null)
			{
				sendNotification(Constants.LOAD_EVENT, selectedItem.name);
			}
		}
		
		private function get view():EventListView
		{
			return viewComponent as EventListView;
		}
	}
}